package com.maoye.ossp.biz.brand.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.brand.entity.BackupBrand;
import com.maoye.ossp.biz.brand.service.IBackupBrandService;
import com.maoye.ossp.biz.brand.service.ISupplierService;
import com.maoye.ossp.biz.brand.utils.BrandUtils;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:供应商代理或经营的品牌 控制器类 
 * 开发公司:maoye shenzhen 
 * 开发人员:lyf 
 */
@Controller
@RequestMapping("/biz/brand/backUpBrand/")
public class BackUpBrandController extends BaseController {
	@Resource
	private IBackupBrandService backupBrandService;
	@Resource
	private ISupplierService supplierService;

	/**
	 * 取得供应商代理或经营的品牌信息分页列表
	 * 
	 * @param supplier
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("list")
	public String list(BackupBrand backupBrand, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception 
	{
		QueryFilter queryFilter = getQuerFilter(request, backupBrand);
		queryFilter.getParams().put("supplierAccountid", null);
		Page<BackupBrand> page = backupBrandService.find(queryFilter);
		model.addAttribute("page", page);
//		List<BackupBrand> list = backupBrandService.getByMainId(BrandCommonConstant.CURRENT_SUPPLIER_MAIN_ID);
//		model.addAttribute("backupBrandlist", list);
		model.addAttribute("CURRENT_SUPPLIER_MAIN_ID", null);
		return resultPage("/biz/brand/backUpBrand/list", model, request);
	}
	/**
	 * 添加供应商代理或经营的品牌信息
	 * 
	 * @param brandLevel
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "add")
	public String add(BackupBrand backupBrand, Model model) {
		model.addAttribute("backupBrand", backupBrand);
		return "/biz/brand/backUpBrand/add";
	}
	/**
	 * 添加或更新供应商代理或经营的品牌信息。
	 * 
	 * @param supplier添加或更新的实体
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(BackupBrand backupBrand, String delScopeId, Model model,
			RedirectAttributes redirectAttributes) {
		try 
		{
			//添加或者修改供应商代理或经营的品牌
			String message=backupBrandService.updateOrAdd(backupBrand, delScopeId);
			addMessage(redirectAttributes,message);
		} 
		catch (Exception e)
		{
			e.printStackTrace();
			addMessage(redirectAttributes, "保存数据失败");
		}
		return "redirect:/biz/brand/supplier/list";
	}

	/**
	 * 查看供应商代理或经营的品牌信息
	 * 
	 * @param id 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) {
		BackupBrand backupBrand=backupBrandService.get(Long.valueOf(id));
		model.addAttribute("backupBrand", backupBrand);
		return "/biz/brand/backUpBrand/view";
	}

	/**
	 * 编辑供应商代理或经营的品牌信息
	 * 
	 * @param id
	 *            子账号id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model,
		RedirectAttributes redirectAttributes) {
		BackupBrand backupBrand=backupBrandService.get(Long.valueOf(id));
		model.addAttribute("backupBrand", backupBrand);
		return "/biz/brand/backUpBrand/add";
	}

	/**
	 * 删除供应商代理或者经营品牌信息
	 * 
	 * @param id 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes)
	{
		try 
		{
			// 删除供应商银行信息
			backupBrandService.delByPk(Long.valueOf(id));
			addMessage(redirectAttributes, "删除数据成功！");
		} catch (NumberFormatException e) {
			addMessage(redirectAttributes, "删除数据失败！");
			e.printStackTrace();
		}
		return "redirect:/biz/brand/backUpBrand/list";
	}
	
	@RequestMapping(value = "deleteAll")
	public String deleteAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) 
	{
		String deleteIds = request.getParameter("deleteIds");
		Long[] lAryId = BrandUtils.getLongAryByStr(deleteIds,";");
		for(Long id:lAryId)
		{
			backupBrandService.delByPk(id);
		}
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/backUpBrand/list";
	}
}
